package com.xymtop.api.controller;

import com.xymtop.api.config.ServerStatus;
import com.xymtop.api.entity.Project;
import com.xymtop.api.service.ProjectService;
import com.xymtop.api.utils.JsonResoult;
import com.xymtop.api.utils.Timer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @RequestMapping("add")
    public JsonResoult<Project> add(@RequestParam("name") String name,@RequestParam("content")String content,@RequestParam("progress") String progress,
                                   @RequestParam("resoult") String resoult) {
        Project project = new Project();
        project.setId(Timer.getTimeStamp().toString());
        project.setName(name);
        project.setContent(content);
        project.setProgress(progress);
        project.setResoult(resoult);
        project.setStatus("1");

        if (projectService.add(project) == 0) {
            return new JsonResoult<>(ServerStatus.Fail);

        } else {
            return new JsonResoult<Project>(ServerStatus.SUCCESS,"",project);

        }
    }

    @RequestMapping("delete")
    public JsonResoult<String> delete(@RequestParam("id") String id){
        if(projectService.delete(id)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }

    @RequestMapping("changecontent")
    public JsonResoult<String> changecontent(@RequestParam("id") String id,@RequestParam("content") String content)
    {
        if(projectService.changecontent(id, content)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }

    @RequestMapping("changProgress")
    public JsonResoult<String> changProgess(@RequestParam("id") String id,@RequestParam("progress") String progress)
    {
        if(projectService.changProgress(id, progress)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }

    @RequestMapping("changeResoult")
    public JsonResoult<String> changeResoult(@RequestParam("id") String id,@RequestParam("resoult") String resoult)
    {
        if(projectService.changProgress(id, resoult)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
    }


    @RequestMapping("getList")
    public JsonResoult<List<Project>> getList(@RequestParam("name") String id) {
        return new JsonResoult<>(200,"",projectService.getList(id));
    }

    @RequestMapping("List")
    public  JsonResoult<List<Project>> List(@RequestParam("num") int num){
        return  new JsonResoult<>(200,"",projectService.List(num));
    }

    @RequestMapping("get")
    public  JsonResoult<Project> List(@RequestParam("id") String id){
        return  new JsonResoult<>(200,"",projectService.get(id));
    }

    @RequestMapping("ChangeProposal")
    public  JsonResoult<String> ChangeProposal(@RequestParam("id") String id,@RequestParam("proposal") String Proposal){
        if(projectService.ChangeProposal(id,Proposal)==0){
            return  new JsonResoult(204);
        }else {
            return  new JsonResoult(200);
        }
    }

    @RequestMapping("ChangePpt")
    public  JsonResoult<String> ChangePpt(@RequestParam("id") String id,@RequestParam("ppt") String ppt){
        if(projectService.ChangePpt(id,ppt)==0){
            return  new JsonResoult(204);
        }else {
            return  new JsonResoult(200);
        }
    }
}


